/*
 * # 应用所有数据库表:
 *  Notifying - 通知
 * 
 * # 描述:
 *  向所有用户提供通知功能，包括页面右下角的未读通知、发送新通知、已发送通知列表以及已接收通知列表。
 * 
 * Copyright (c), 上海沃恩信息科技有限公司.
 * All rights reserved.
 * Licensed under BSD (https://www.pittypat.work/bsd.html).
 */

use "pittypat_app";
go

-- 确保架构存在
if schema_id('waln_notifying') is null
	exec('create schema "waln_notifying" authorization "dbo"');
go

/*
 * # 辅助函数:
 *  _pittypat_table_exists - 检查指定的表是否已经存在
 * 
 * # 描述:
 *  检查指定的表是否已经存在。
 */

if not exists (select * from "sys"."objects" where "object_id" = object_id(N'"waln_notifying"."_pittypat_table_exists"') and "type" in (N'FN', N'IF', N'TF', N'FS', N'FT'))
	exec('create function "waln_notifying"."_pittypat_table_exists"() returns int begin return 0; end');
go

alter function "waln_notifying"."_pittypat_table_exists"(@tbl varchar(30))
returns bit
as
begin
	if @tbl is not null and object_id(N'"waln_notifying".' + @tbl, N'U') is not null
		return 1;
	return 0;
end;
go

/*
 * # 辅助函数:
 *  _pittypat_column_exists - 检查指定的列是否已经存在
 * 
 * # 描述:
 *  检查指定的列是否已经存在。
 */

if not exists (select * from "sys"."objects" where "object_id" = object_id(N'"waln_notifying"."_pittypat_column_exists"') and "type" in (N'FN', N'IF', N'TF', N'FS', N'FT'))
	exec('create function "waln_notifying"."_pittypat_column_exists"() returns int begin return 0; end');
go

alter function "waln_notifying"."_pittypat_column_exists"(@tbl varchar(30), @col varchar(30))
returns bit
as
begin
	if @tbl is not null and @col is not null and 
		exists (select * from "sys"."columns" where "object_id" = object_id(N'[waln_notifying].' + @tbl, N'U') and "name" = @col)
		return 1;
	return 0;
end;
go

/*
 * # 辅助函数:
 *  _pittypat_index_exists - 检查指定的索引是否已经存在
 * 
 * # 描述:
 *  检查指定的索引是否已经存在。
 */

if not exists (select * from "sys"."objects" where "object_id" = object_id(N'"waln_notifying"."_pittypat_index_exists"') and "type" in (N'FN', N'IF', N'TF', N'FS', N'FT'))
	exec('create function "waln_notifying"."_pittypat_index_exists"() returns int begin return 0; end');
go

alter function "waln_notifying"."_pittypat_index_exists"(@tbl varchar(30), @idx varchar(63))
returns bit
as
begin
	if @tbl is not null and @idx is not null and 
		exists (select * from "sys"."indexes" where "object_id" = object_id(N'"waln_notifying".' + @tbl, N'U') and "name" = @idx)
		return 1;
	return 0;
end;
go

/*
 * # 数据库表 - Sql Server:
 *  Notifications - 通知
 * 
 * # 描述:
 *  一条已经发送或接收到的通知消息。
 */

if not exists (select * from "sys"."objects" where "object_id" = object_id(N'"waln_notifying"."_pittypat_replace_table"') and "type" in (N'P', N'PC'))
	exec('create procedure "waln_notifying"."_pittypat_replace_table" as begin set nocount on; end');
go

alter procedure "waln_notifying"."_pittypat_replace_table"
as
begin
	set nocount on;

	if "waln_notifying"."_pittypat_table_exists"('"Notifications"') = 0
	begin
		create table "waln_notifying"."Notifications"
		(
			"PK" bigint not null identity(1, 1), -- 主键
			"Id" uniqueidentifier not null, -- Id
			"SenderId" uniqueidentifier null, -- 发送用户的 Id
			"ReceiverId" uniqueidentifier null, -- 接收用户的 Id
			"ReceiveTime" datetime2 not null, -- 接收时间
			"ReadTime" datetime2 null, -- 阅读时间
			"TopTime" datetime2 null, -- 置顶时间
			"Urgency" tinyint not null, -- 紧急程度
			"Group" nvarchar(30) null, -- 分组
			"Title" nvarchar(50) null, -- 通知标题
			"Summary" nvarchar(200) null, -- 通知消息摘要
			"Body" nvarchar(max) null, -- 通知正文内容
			"BodyType" tinyint not null, -- 正文类型代码
			"FuncUrl" nvarchar(300) null, -- 目标功能路径
			"RefDataType" nvarchar(64) null, -- 引用数据的类型
			"RefDataKey" nvarchar(50) null, -- 引用数据键值
			"AttachmentId" uniqueidentifier null -- 附件 Id
		)
		on "primary";

		-- 索引 K_Notifications
		alter table "waln_notifying"."Notifications" add constraint
		"K_Notifications" primary key clustered
		(
			"PK" asc
		)
		with
		(
			statistics_norecompute = off,
			ignore_dup_key = off,
			allow_row_locks = on,
			allow_page_locks = on
		)
		on "primary";

		-- 索引 U_Notifications_Id
		create unique nonclustered index "U_Notifications_Id"
		on "waln_notifying"."Notifications"
		(
			"Id" asc
		)
		with
		(
			statistics_norecompute = off,
			ignore_dup_key = off,
			allow_row_locks = on,
			allow_page_locks = on
		)
		on "primary";

		-- 索引 I_Notifications_Sender
		create nonclustered index "I_Notifications_Sender"
		on "waln_notifying"."Notifications"
		(
			"SenderId" asc
		)
		with
		(
			statistics_norecompute = off,
			ignore_dup_key = off,
			allow_row_locks = on,
			allow_page_locks = on
		)
		on "primary";

		-- 索引 I_Notifications_Receiver
		create nonclustered index "I_Notifications_Receiver"
		on "waln_notifying"."Notifications"
		(
			"ReceiverId" asc
		)
		with
		(
			statistics_norecompute = off,
			ignore_dup_key = off,
			allow_row_locks = on,
			allow_page_locks = on
		)
		on "primary";

		-- 索引 I_Notifications_ReadTime
		create nonclustered index "I_Notifications_ReadTime"
		on "waln_notifying"."Notifications"
		(
			"ReadTime" asc
		)
		with
		(
			statistics_norecompute = off,
			ignore_dup_key = off,
			allow_row_locks = on,
			allow_page_locks = on
		)
		on "primary";

		-- 索引 I_Notifications_TopTime
		create nonclustered index "I_Notifications_TopTime"
		on "waln_notifying"."Notifications"
		(
			"TopTime" asc
		)
		with
		(
			statistics_norecompute = off,
			ignore_dup_key = off,
			allow_row_locks = on,
			allow_page_locks = on
		)
		on "primary";

		-- 索引 I_Notifications_RefData
		create nonclustered index "I_Notifications_RefData"
		on "waln_notifying"."Notifications"
		(
			"RefDataType" asc,
			"RefDataKey" asc
		)
		with
		(
			statistics_norecompute = off,
			ignore_dup_key = off,
			allow_row_locks = on,
			allow_page_locks = on
		)
		on "primary";

		-- 索引 I_Notifications_Attachment
		create nonclustered index "I_Notifications_Attachment"
		on "waln_notifying"."Notifications"
		(
			"AttachmentId" asc
		)
		with
		(
			statistics_norecompute = off,
			ignore_dup_key = off,
			allow_row_locks = on,
			allow_page_locks = on
		)
		on "primary";
	end
	else
	begin
		-- 列 Id 
		if "waln_notifying"."_pittypat_column_exists"('Notifications', 'Id') = 0
			alter table "waln_notifying"."Notifications" add
			"Id" uniqueidentifier not null;
		else
			alter table "waln_notifying"."Notifications" alter column
			"Id" uniqueidentifier not null;

		-- 列 SenderId 
		if "waln_notifying"."_pittypat_column_exists"('Notifications', 'SenderId') = 0
			alter table "waln_notifying"."Notifications" add
			"SenderId" uniqueidentifier null;
		else
			alter table "waln_notifying"."Notifications" alter column
			"SenderId" uniqueidentifier null;

		-- 列 ReceiverId 
		if "waln_notifying"."_pittypat_column_exists"('Notifications', 'ReceiverId') = 0
			alter table "waln_notifying"."Notifications" add
			"ReceiverId" uniqueidentifier null;
		else
			alter table "waln_notifying"."Notifications" alter column
			"ReceiverId" uniqueidentifier null;

		-- 列 ReceiveTime 
		if "waln_notifying"."_pittypat_column_exists"('Notifications', 'ReceiveTime') = 0
			alter table "waln_notifying"."Notifications" add
			"ReceiveTime" datetime2 not null;
		else
			alter table "waln_notifying"."Notifications" alter column
			"ReceiveTime" datetime2 not null;

		-- 列 ReadTime 
		if "waln_notifying"."_pittypat_column_exists"('Notifications', 'ReadTime') = 0
			alter table "waln_notifying"."Notifications" add
			"ReadTime" datetime2 null;
		else
			alter table "waln_notifying"."Notifications" alter column
			"ReadTime" datetime2 null;

		-- 列 TopTime 
		if "waln_notifying"."_pittypat_column_exists"('Notifications', 'TopTime') = 0
			alter table "waln_notifying"."Notifications" add
			"TopTime" datetime2 null;
		else
			alter table "waln_notifying"."Notifications" alter column
			"TopTime" datetime2 null;

		-- 列 Urgency 
		if "waln_notifying"."_pittypat_column_exists"('Notifications', 'Urgency') = 0
			alter table "waln_notifying"."Notifications" add
			"Urgency" tinyint not null;
		else
			alter table "waln_notifying"."Notifications" alter column
			"Urgency" tinyint not null;

		-- 列 Group 
		if "waln_notifying"."_pittypat_column_exists"('Notifications', 'Group') = 0
			alter table "waln_notifying"."Notifications" add
			"Group" nvarchar(30) null;
		else
			alter table "waln_notifying"."Notifications" alter column
			"Group" nvarchar(30) null;

		-- 列 Title 
		if "waln_notifying"."_pittypat_column_exists"('Notifications', 'Title') = 0
			alter table "waln_notifying"."Notifications" add
			"Title" nvarchar(50) null;
		else
			alter table "waln_notifying"."Notifications" alter column
			"Title" nvarchar(50) null;

		-- 列 Summary 
		if "waln_notifying"."_pittypat_column_exists"('Notifications', 'Summary') = 0
			alter table "waln_notifying"."Notifications" add
			"Summary" nvarchar(200) null;
		else
			alter table "waln_notifying"."Notifications" alter column
			"Summary" nvarchar(200) null;

		-- 列 Body 
		if "waln_notifying"."_pittypat_column_exists"('Notifications', 'Body') = 0
			alter table "waln_notifying"."Notifications" add
			"Body" nvarchar(max) null;
		else
			alter table "waln_notifying"."Notifications" alter column
			"Body" nvarchar(max) null;

		-- 列 BodyType 
		if "waln_notifying"."_pittypat_column_exists"('Notifications', 'BodyType') = 0
			alter table "waln_notifying"."Notifications" add
			"BodyType" tinyint not null;
		else
			alter table "waln_notifying"."Notifications" alter column
			"BodyType" tinyint not null;

		-- 列 FuncUrl 
		if "waln_notifying"."_pittypat_column_exists"('Notifications', 'FuncUrl') = 0
			alter table "waln_notifying"."Notifications" add
			"FuncUrl" nvarchar(300) null;
		else
			alter table "waln_notifying"."Notifications" alter column
			"FuncUrl" nvarchar(300) null;

		-- 列 RefDataType 
		if "waln_notifying"."_pittypat_column_exists"('Notifications', 'RefDataType') = 0
			alter table "waln_notifying"."Notifications" add
			"RefDataType" nvarchar(64) null;
		else
			alter table "waln_notifying"."Notifications" alter column
			"RefDataType" nvarchar(64) null;

		-- 列 RefDataKey 
		if "waln_notifying"."_pittypat_column_exists"('Notifications', 'RefDataKey') = 0
			alter table "waln_notifying"."Notifications" add
			"RefDataKey" nvarchar(50) null;
		else
			alter table "waln_notifying"."Notifications" alter column
			"RefDataKey" nvarchar(50) null;

		-- 列 AttachmentId 
		if "waln_notifying"."_pittypat_column_exists"('Notifications', 'AttachmentId') = 0
			alter table "waln_notifying"."Notifications" add
			"AttachmentId" uniqueidentifier null;
		else
			alter table "waln_notifying"."Notifications" alter column
			"AttachmentId" uniqueidentifier null;

		-- 删除索引 U_Notifications_Id
		if "waln_notifying"."_pittypat_index_exists"('Notifications', 'U_Notifications_Id') = 1
			drop index "U_Notifications_Id" on "waln_notifying"."Notifications";

		-- 创建索引 U_Notifications_Id
		create unique nonclustered index "U_Notifications_Id"
		on "waln_notifying"."Notifications"
		(
			"Id" asc
		)
		with
		(
			statistics_norecompute = off,
			ignore_dup_key = off,
			allow_row_locks = on,
			allow_page_locks = on
		)
		on "primary";

		-- 删除索引 I_Notifications_Sender
		if "waln_notifying"."_pittypat_index_exists"('Notifications', 'I_Notifications_Sender') = 1
			drop index "I_Notifications_Sender" on "waln_notifying"."Notifications";

		-- 创建索引 I_Notifications_Sender
		create nonclustered index "I_Notifications_Sender"
		on "waln_notifying"."Notifications"
		(
			"SenderId" asc
		)
		with
		(
			statistics_norecompute = off,
			ignore_dup_key = off,
			allow_row_locks = on,
			allow_page_locks = on
		)
		on "primary";

		-- 删除索引 I_Notifications_Receiver
		if "waln_notifying"."_pittypat_index_exists"('Notifications', 'I_Notifications_Receiver') = 1
			drop index "I_Notifications_Receiver" on "waln_notifying"."Notifications";

		-- 创建索引 I_Notifications_Receiver
		create nonclustered index "I_Notifications_Receiver"
		on "waln_notifying"."Notifications"
		(
			"ReceiverId" asc
		)
		with
		(
			statistics_norecompute = off,
			ignore_dup_key = off,
			allow_row_locks = on,
			allow_page_locks = on
		)
		on "primary";

		-- 删除索引 I_Notifications_ReadTime
		if "waln_notifying"."_pittypat_index_exists"('Notifications', 'I_Notifications_ReadTime') = 1
			drop index "I_Notifications_ReadTime" on "waln_notifying"."Notifications";

		-- 创建索引 I_Notifications_ReadTime
		create nonclustered index "I_Notifications_ReadTime"
		on "waln_notifying"."Notifications"
		(
			"ReadTime" asc
		)
		with
		(
			statistics_norecompute = off,
			ignore_dup_key = off,
			allow_row_locks = on,
			allow_page_locks = on
		)
		on "primary";

		-- 删除索引 I_Notifications_TopTime
		if "waln_notifying"."_pittypat_index_exists"('Notifications', 'I_Notifications_TopTime') = 1
			drop index "I_Notifications_TopTime" on "waln_notifying"."Notifications";

		-- 创建索引 I_Notifications_TopTime
		create nonclustered index "I_Notifications_TopTime"
		on "waln_notifying"."Notifications"
		(
			"TopTime" asc
		)
		with
		(
			statistics_norecompute = off,
			ignore_dup_key = off,
			allow_row_locks = on,
			allow_page_locks = on
		)
		on "primary";

		-- 删除索引 I_Notifications_RefData
		if "waln_notifying"."_pittypat_index_exists"('Notifications', 'I_Notifications_RefData') = 1
			drop index "I_Notifications_RefData" on "waln_notifying"."Notifications";

		-- 创建索引 I_Notifications_RefData
		create nonclustered index "I_Notifications_RefData"
		on "waln_notifying"."Notifications"
		(
			"RefDataType" asc,
			"RefDataKey" asc
		)
		with
		(
			statistics_norecompute = off,
			ignore_dup_key = off,
			allow_row_locks = on,
			allow_page_locks = on
		)
		on "primary";

		-- 删除索引 I_Notifications_Attachment
		if "waln_notifying"."_pittypat_index_exists"('Notifications', 'I_Notifications_Attachment') = 1
			drop index "I_Notifications_Attachment" on "waln_notifying"."Notifications";

		-- 创建索引 I_Notifications_Attachment
		create nonclustered index "I_Notifications_Attachment"
		on "waln_notifying"."Notifications"
		(
			"AttachmentId" asc
		)
		with
		(
			statistics_norecompute = off,
			ignore_dup_key = off,
			allow_row_locks = on,
			allow_page_locks = on
		)
		on "primary";
	end
end
go

exec "waln_notifying"."_pittypat_replace_table";
go

/*
	select
		"Id", -- Id
		"SenderId", -- 发送用户的 Id
		"ReceiverId", -- 接收用户的 Id
		"ReceiveTime", -- 接收时间
		"ReadTime", -- 阅读时间
		"TopTime", -- 置顶时间
		"Urgency", -- 紧急程度
		"Group", -- 分组
		"Title", -- 通知标题
		"Summary", -- 通知消息摘要
		"Body", -- 通知正文内容
		"BodyType", -- 正文类型代码
		"FuncUrl", -- 目标功能路径
		"RefDataType", -- 引用数据的类型
		"RefDataKey", -- 引用数据键值
		"AttachmentId"  -- 附件 Id
	from
		"waln_notifying"."Notifications"
	where
		"Id" = @id;

	insert into "waln_notifying"."Notifications"
	(
		"Id", -- Id
		"SenderId", -- 发送用户的 Id
		"ReceiverId", -- 接收用户的 Id
		"ReceiveTime", -- 接收时间
		"ReadTime", -- 阅读时间
		"TopTime", -- 置顶时间
		"Urgency", -- 紧急程度
		"Group", -- 分组
		"Title", -- 通知标题
		"Summary", -- 通知消息摘要
		"Body", -- 通知正文内容
		"BodyType", -- 正文类型代码
		"FuncUrl", -- 目标功能路径
		"RefDataType", -- 引用数据的类型
		"RefDataKey", -- 引用数据键值
		"AttachmentId"  -- 附件 Id
	)
	values
	(
		@id, -- Id
		@senderId, -- 发送用户的 Id
		@receiverId, -- 接收用户的 Id
		@receiveTime, -- 接收时间
		@readTime, -- 阅读时间
		@topTime, -- 置顶时间
		@urgency, -- 紧急程度
		@group, -- 分组
		@title, -- 通知标题
		@summary, -- 通知消息摘要
		@body, -- 通知正文内容
		@bodyType, -- 正文类型代码
		@funcUrl, -- 目标功能路径
		@refDataType, -- 引用数据的类型
		@refDataKey, -- 引用数据键值
		@attachmentId  -- 附件 Id
	);

	update
		"waln_notifying"."Notifications"
	set
		"Id" = @id, -- Id
		"SenderId" = @senderId, -- 发送用户的 Id
		"ReceiverId" = @receiverId, -- 接收用户的 Id
		"ReceiveTime" = @receiveTime, -- 接收时间
		"ReadTime" = @readTime, -- 阅读时间
		"TopTime" = @topTime, -- 置顶时间
		"Urgency" = @urgency, -- 紧急程度
		"Group" = @group, -- 分组
		"Title" = @title, -- 通知标题
		"Summary" = @summary, -- 通知消息摘要
		"Body" = @body, -- 通知正文内容
		"BodyType" = @bodyType, -- 正文类型代码
		"FuncUrl" = @funcUrl, -- 目标功能路径
		"RefDataType" = @refDataType, -- 引用数据的类型
		"RefDataKey" = @refDataKey, -- 引用数据键值
		"AttachmentId" = @attachmentId  -- 附件 Id
	where
		"Id" = @id;

	delete from
		"waln_notifying"."Notifications"
	where
		"Id" = @id;
*/

/*
 * # 数据库表 - Sql Server:
 *  Attachments - 通知附件
 * 
 * # 描述:
 *  表示接收消息的一个附件文件。
 */

if not exists (select * from "sys"."objects" where "object_id" = object_id(N'"waln_notifying"."_pittypat_replace_table"') and "type" in (N'P', N'PC'))
	exec('create procedure "waln_notifying"."_pittypat_replace_table" as begin set nocount on; end');
go

alter procedure "waln_notifying"."_pittypat_replace_table"
as
begin
	set nocount on;

	if "waln_notifying"."_pittypat_table_exists"('"Attachments"') = 0
	begin
		create table "waln_notifying"."Attachments"
		(
			"PK" bigint not null identity(1, 1), -- 主键
			"Id" uniqueidentifier not null, -- Id
			"Name" nvarchar(100) not null, -- 文件名称
			"Mime" nvarchar(100) null, -- MIME 类型
			"Size" bigint not null, -- 附件文件的大小
			"Url" nvarchar(300) null, -- 文件路径
			"UploaderId" uniqueidentifier not null, -- 上传人的 Id
			"UploadTime" datetime2 not null -- 上传时间
		)
		on "primary";

		-- 索引 K_Attachments
		alter table "waln_notifying"."Attachments" add constraint
		"K_Attachments" primary key clustered
		(
			"PK" asc
		)
		with
		(
			statistics_norecompute = off,
			ignore_dup_key = off,
			allow_row_locks = on,
			allow_page_locks = on
		)
		on "primary";

		-- 索引 U_Attachments_Id
		create unique nonclustered index "U_Attachments_Id"
		on "waln_notifying"."Attachments"
		(
			"Id" asc
		)
		with
		(
			statistics_norecompute = off,
			ignore_dup_key = off,
			allow_row_locks = on,
			allow_page_locks = on
		)
		on "primary";
	end
	else
	begin
		-- 列 Id 
		if "waln_notifying"."_pittypat_column_exists"('Attachments', 'Id') = 0
			alter table "waln_notifying"."Attachments" add
			"Id" uniqueidentifier not null;
		else
			alter table "waln_notifying"."Attachments" alter column
			"Id" uniqueidentifier not null;

		-- 列 Name 
		if "waln_notifying"."_pittypat_column_exists"('Attachments', 'Name') = 0
			alter table "waln_notifying"."Attachments" add
			"Name" nvarchar(100) not null;
		else
			alter table "waln_notifying"."Attachments" alter column
			"Name" nvarchar(100) not null;

		-- 列 Mime 
		if "waln_notifying"."_pittypat_column_exists"('Attachments', 'Mime') = 0
			alter table "waln_notifying"."Attachments" add
			"Mime" nvarchar(100) null;
		else
			alter table "waln_notifying"."Attachments" alter column
			"Mime" nvarchar(100) null;

		-- 列 Size 
		if "waln_notifying"."_pittypat_column_exists"('Attachments', 'Size') = 0
			alter table "waln_notifying"."Attachments" add
			"Size" bigint not null;
		else
			alter table "waln_notifying"."Attachments" alter column
			"Size" bigint not null;

		-- 列 Url 
		if "waln_notifying"."_pittypat_column_exists"('Attachments', 'Url') = 0
			alter table "waln_notifying"."Attachments" add
			"Url" nvarchar(300) null;
		else
			alter table "waln_notifying"."Attachments" alter column
			"Url" nvarchar(300) null;

		-- 列 UploaderId 
		if "waln_notifying"."_pittypat_column_exists"('Attachments', 'UploaderId') = 0
			alter table "waln_notifying"."Attachments" add
			"UploaderId" uniqueidentifier not null;
		else
			alter table "waln_notifying"."Attachments" alter column
			"UploaderId" uniqueidentifier not null;

		-- 列 UploadTime 
		if "waln_notifying"."_pittypat_column_exists"('Attachments', 'UploadTime') = 0
			alter table "waln_notifying"."Attachments" add
			"UploadTime" datetime2 not null;
		else
			alter table "waln_notifying"."Attachments" alter column
			"UploadTime" datetime2 not null;

		-- 删除索引 U_Attachments_Id
		if "waln_notifying"."_pittypat_index_exists"('Attachments', 'U_Attachments_Id') = 1
			drop index "U_Attachments_Id" on "waln_notifying"."Attachments";

		-- 创建索引 U_Attachments_Id
		create unique nonclustered index "U_Attachments_Id"
		on "waln_notifying"."Attachments"
		(
			"Id" asc
		)
		with
		(
			statistics_norecompute = off,
			ignore_dup_key = off,
			allow_row_locks = on,
			allow_page_locks = on
		)
		on "primary";
	end
end
go

exec "waln_notifying"."_pittypat_replace_table";
go

/*
	select
		"Id", -- Id
		"Name", -- 文件名称
		"Mime", -- MIME 类型
		"Size", -- 附件文件的大小
		"Url", -- 文件路径
		"UploaderId", -- 上传人的 Id
		"UploadTime"  -- 上传时间
	from
		"waln_notifying"."Attachments"
	where
		"Id" = @id;

	insert into "waln_notifying"."Attachments"
	(
		"Id", -- Id
		"Name", -- 文件名称
		"Mime", -- MIME 类型
		"Size", -- 附件文件的大小
		"Url", -- 文件路径
		"UploaderId", -- 上传人的 Id
		"UploadTime"  -- 上传时间
	)
	values
	(
		@id, -- Id
		@name, -- 文件名称
		@mime, -- MIME 类型
		@size, -- 附件文件的大小
		@url, -- 文件路径
		@uploaderId, -- 上传人的 Id
		@uploadTime  -- 上传时间
	);

	update
		"waln_notifying"."Attachments"
	set
		"Id" = @id, -- Id
		"Name" = @name, -- 文件名称
		"Mime" = @mime, -- MIME 类型
		"Size" = @size, -- 附件文件的大小
		"Url" = @url, -- 文件路径
		"UploaderId" = @uploaderId, -- 上传人的 Id
		"UploadTime" = @uploadTime  -- 上传时间
	where
		"Id" = @id;

	delete from
		"waln_notifying"."Attachments"
	where
		"Id" = @id;
*/
